<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class LogosModel extends JLModelBase {

    function insert($var_title, $txt_description, $dat_creation, $var_link, $var_src, $var_size, $int_id_state, $id_int_partenaire, $int_order) {
        $var_title = $this->DB->Quote($var_title);
        $txt_description = $this->DB->Quote($txt_description);
        $dat_creation = $this->DB->Quote($dat_creation);
        $var_link = $this->DB->Quote($var_link);
        $var_src = $this->DB->Quote($var_src);
        $var_size = $this->DB->Quote($var_size);
        $int_id_state = $this->DB->Quote($int_id_state);
        $id_int_partenaire = $this->DB->Quote($id_int_partenaire);
        $int_order = $this->DB->Quote($int_order);

        $sql = "INSERT INTO `tbl_logos` (
              `var_title`,
              `txt_description`,
              `dat_creation`,
              `var_link`,
              `var_src`,
              `var_size`,
              `int_id_state`,
              `id_int_partenaire`,
              `int_order`
            ) 
            VALUE(
              " . $var_title . ",
              " . $txt_description . ",
              " . $dat_creation . ",
              " . $var_link . ",
              " . $var_src . ",
              " . $var_size . ",
              " . $int_id_state . ",
              " . $id_int_partenaire . ",
              " . $int_order . "
            )";

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? true : false;
    }

    function getLogo($id) {
        $sql = "SELECT 
              `int_id_logo`,
              `var_title`,
              `txt_description`,
              `dat_creation`,
              `var_link`,
              `var_src`,
              `var_size`,
              `int_id_state`,
              `id_int_partenaire`,
              `int_order`
            FROM 
              `tbl_logos`
            WHERE int_id_logo = " . $this->DB->Quote($id);

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? $this->DB->getDataArray() : null;
    }

    function countLogoByIdPart($id) {
        $sql = "SELECT 
              COUNT(`int_id_logo`) AS COUNT
            FROM 
              `tbl_logos`
            WHERE id_int_partenaire = " . $this->DB->Quote($id);

        $this->DB->executeQuery($sql);
        $data = $this->DB->getDataArray();

        return intval($data[0]['COUNT']);
    }

    function getLogosByIdPart($id) {
        $sql = "SELECT 
              `int_id_logo`,
              `var_title`,
              `var_src`,
              `var_size`,
              `id_int_partenaire`
            FROM 
              `tbl_logos`
            WHERE id_int_partenaire = " . $this->DB->Quote($id) . " AND int_id_state <> 3
            ORDER BY int_order ASC";

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? $this->DB->getDataArray() : null;
    }

    function update($int_id_logo, $var_title, $txt_description, $var_link, $var_src, $var_size, $int_id_state, $id_int_partenaire) {
        $sql = "UPDATE 
              `tbl_logos`
            SET 
              `var_title` = " . $this->DB->Quote($var_title) . ",
              `txt_description` = " . $this->DB->Quote($txt_description) . ",
              `var_link` = " . $this->DB->Quote($var_link) . ",
              `var_src` = " . $this->DB->Quote($var_src) . ",
              `var_size` = " . $this->DB->Quote($var_size) . ",
              `int_id_state` = " . $this->DB->Quote($int_id_state) . ",
              `id_int_partenaire` = " . $this->DB->Quote($id_int_partenaire) . "
            WHERE 
              `int_id_logo` = " . $this->DB->Quote($int_id_logo);

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? true : false;
    }

    function updateStateDesactive($int_id_logo, $id_int_partenaire) {
        $sql = "UPDATE 
              `tbl_logos`
            SET 
              `int_id_state` = 3
            WHERE 
              `int_id_logo` <> " . $this->DB->Quote($int_id_logo) . " AND `id_int_partenaire` = " . $this->DB->Quote($id_int_partenaire);

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? true : false;
    }

    function updateSort($int_id_logo, $int_order) {
        $sql = "UPDATE 
              `tbl_logos`
            SET 
              `int_order` = " . $this->DB->Quote($int_order) . "
            WHERE 
              `int_id_logo` = " . $this->DB->Quote($int_id_logo);

        $this->DB->executeQuery($sql);
        return ($this->DB->getRowsAffected() > 0) ? true : false;
    }

    function delete($int_id_logo) {
        $sql = "DELETE FROM 
              `tbl_logos` 
            WHERE 
              `int_id_logo` = " . $this->DB->Quote($int_id_logo) . " AND `int_id_state` <> 1";
        $this->DB->executeQuery($sql);

        return ($this->DB->getRowsAffected() > 0) ? true : false;
    }

}